//
//  QQAddressVC.m
//  QQZF
//
//  Created by Henry on 2018/6/30.
//  Copyright © 2018年 com.szqqkj. All rights reserved.
//

#import "QQAddressVC.h"
#import "HGAddressModel.h"

@interface QQAddressVC ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (strong, nonatomic) UIView *settingView;

@property (strong, nonatomic) UIPickerView *pickerView;

@property (nonatomic, strong) NSMutableArray<HGAddressModel *> *firstDataSource;
@property (nonatomic, strong) NSMutableArray<HGAddressModel *> *secondDataSource;
@property (nonatomic, strong) NSMutableArray<HGAddressModel *> *thirdDataSource;

@property (nonatomic, assign) NSInteger firstIndex;
@property (nonatomic, assign) NSInteger secondIndex;
@property (nonatomic, assign) NSInteger thirdIndex;

@end

@implementation QQAddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initSubViews];
    
    [self initDatawithID:@"1" withComponent:10000];
}

- (void)initSubViews {
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    
    [self.view addSubview:self.settingView];
    [self.view addSubview:self.pickerView];
}

//分区个数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}
//每个分区row个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.firstDataSource.count;
    }else if (component == 1) {
        return self.secondDataSource.count;
    }else {
        return self.thirdDataSource.count;
    }
}

#pragma mark - UIPickerViewDelegate
//滚动点击row
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED{
    if (component == 0) {
        _firstIndex = row;
        [self initDatawithID:_firstDataSource[row].regionId withComponent:component];
    } else if (component == 1) {
        _secondIndex = row;
        [self initDatawithID:_secondDataSource[row].regionId withComponent:component];
    } else if (component == 2) {
        _thirdIndex = row;
    }
}

//row显示内容
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED{
    if (component == 0) {
        return _firstDataSource[row].name;
    }else if (component == 1) {
        return _secondDataSource[row].name;
    }else {
        return _thirdDataSource[row].name;
    }
}
//row高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 35;
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *lbl = (UILabel *)view;
    if (lbl != nil) return lbl;
    lbl = [[UILabel alloc]init];
    lbl.font = [UIFont systemFontOfSize:15];
    [lbl setTextAlignment:NSTextAlignmentCenter];
    [lbl setBackgroundColor:[UIColor clearColor]];
    lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return lbl;
}

- (void)initDatawithID:(NSString *)ID withComponent:(NSInteger)component {
    
    [[LMJRequestManager sharedManager] GET:KBaseURL(@"app/region/list") parameters:@{@"regionId" : [NSNumber numberWithInteger:[ID integerValue]]} completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            if (component == 0) {
                self.secondDataSource = [HGAddressModel mj_objectArrayWithKeyValuesArray:response.responseModel.data];
                [self.pickerView reloadComponent:1];
                [_pickerView selectRow:0 inComponent:1 animated:YES];
                [self initDatawithID:_secondDataSource.firstObject.regionId withComponent:1];
            } else if (component == 1) {
                self.thirdDataSource = [HGAddressModel mj_objectArrayWithKeyValuesArray:response.responseModel.data];
                [self.pickerView reloadComponent:2];
                [_pickerView selectRow:0 inComponent:2 animated:YES];
            }
            if (component == 10000) {
                self.firstDataSource = [HGAddressModel mj_objectArrayWithKeyValuesArray:response.responseModel.data];
                [self.pickerView reloadComponent:0];
                [self initDatawithID:self.firstDataSource.firstObject.regionId withComponent:0];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    if(_secondDataSource.count == 0) return;
                    [self initDatawithID:self.secondDataSource.firstObject.regionId withComponent:1];
                });
            }
        }
    }];
}

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 222, kScreenWidth, 222)];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        _pickerView.backgroundColor = kColorBackground;
    }
    return _pickerView;
}

- (UIView *)settingView {
    if (!_settingView) {
        _settingView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 262, kScreenWidth, 40)];
        _settingView.backgroundColor = [UIColor whiteColor];
        UIButton *cannleBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 0, 40,40)];
        [cannleBtn addTarget:self action:@selector(actionCilK:) forControlEvents:(UIControlEventTouchUpInside)];
        cannleBtn.tag = 1000;
        [cannleBtn setTitle:@"取消" forState:(UIControlStateNormal)];
        cannleBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [cannleBtn setTitleColor:kColorWithFloat(0x333333) forState:(UIControlStateNormal)];
        
        UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 55, 0, 40,40)];
        [confirmBtn addTarget:self action:@selector(actionCilK:) forControlEvents:(UIControlEventTouchUpInside)];
        [confirmBtn setTitle:@"确认" forState:(UIControlStateNormal)];
        confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [confirmBtn setTitleColor:kColorWithFloat(0x007AFF) forState:(UIControlStateNormal)];
        [_settingView addSubview:cannleBtn];
        [_settingView addSubview:confirmBtn];
    }
    return _settingView;
}

- (void)actionCilK:(UIButton *)sender {
    
    if (sender.tag == 1000) {
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    
    if (self.returnData) {
        
        NSString *regionId = _thirdDataSource.count == 0 ? @"" : _thirdDataSource[_thirdIndex].regionId;
        NSString *area = _thirdDataSource.count == 0 ? @"" : _thirdDataSource[_thirdIndex].name;
        NSString *cityStr = [NSString stringWithFormat:@"%@,%@,%@",_firstDataSource[_firstIndex].regionId,_secondDataSource[_secondIndex].regionId,regionId];
        NSString *city = [NSString stringWithFormat:@"%@ %@ %@",_firstDataSource[_firstIndex].name,_secondDataSource[_secondIndex].name,area];
        
        self.returnData(city,cityStr);
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
